<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/1/30
 * Time: 21:09
 */
namespace App\Repositories\Eloquent;

use App\Lib\BankMap;
use App\Lib\BillTypeMap;
use App\Lib\SystemNumber;
use App\Models\FinanceBill;
use App\Models\RechargeOrder;
use App\Models\RemitOrder;
use Prettus\Repository\Eloquent\BaseRepository;

class FinanceBillRepository extends BaseRepository
{

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        // TODO: Implement model() method.
        return FinanceBill::class;
    }


    /**
     * 记录商户充值订单流水
     *
     * @param RechargeOrder $rechargeOrder
     * @return bool
     */
    public function createMchROBillRecord(RechargeOrder $rechargeOrder)
    {
        $bill_info = "商户订单号[%s]充值成功，订单金额[%s]，通道[%s][%s]，手续费[%s]，结算金额[%s]";
        $fee = bcsub($rechargeOrder->order_amt, $rechargeOrder->order_settle, 6);
        $data = [
            'bill_id' => SystemNumber::getBillRecordNumber(BillTypeMap::RECHARGE),
            'bill_uid' => $rechargeOrder->uid,
            'bill_type' => 2,
            'bill_channel' => 1,
            'channel_id' => $rechargeOrder->plat_no,
            'bill_amt' => $rechargeOrder->order_settle,
            'cur_available' => $rechargeOrder->platuser->assets->available,
            'bill_info' => sprintf($bill_info,
                $rechargeOrder->merchant_no, $rechargeOrder->order_amt, $rechargeOrder->payment->name,
                $rechargeOrder->order_rate .'%', $fee, $rechargeOrder->order_settle),
            'created_at' => $rechargeOrder->updated_at
        ];
        if ($this->create($data)) {
            return true;
        }
        return false;
    }

    /**
     * 记录充值订单代理流水
     *
     * @param RechargeOrder $rechargeOrder
     * @return bool
     */
    public function createProxyROBillRecord(RechargeOrder $rechargeOrder)
    {
        $bill_info = "下级商户[%s]的商户订单号[%s]充值成功，订单金额[%s]，通道[%s]，所结算金额[%s]";
        if (bccomp($rechargeOrder->proxy_settle, $rechargeOrder->order_settle, 6) == 1) {
            $bill_amt = bcsub($rechargeOrder->proxy_settle, $rechargeOrder->order_settle, 6);
            $data = [
                'bill_id' => SystemNumber::getBillRecordNumber(BillTypeMap::RECHARGE),
                'bill_uid' => $rechargeOrder->proxy,
                'bill_type' => 2,
                'bill_channel' => 1,
                'channel_id' => $rechargeOrder->plat_no,
                'bill_amt' => $bill_amt,
                'cur_available' => $rechargeOrder->proxyUser->assets->available,
                'bill_info' => sprintf($bill_info,
                    $rechargeOrder->platuser->code, $rechargeOrder->merchant_no, $rechargeOrder->order_amt,
                    $rechargeOrder->payment->name, $bill_amt),
                'created_at' => $rechargeOrder->updated_at
            ];
            if ($this->create($data)) {
                return true;
            }
        }

        return false;
    }

    public function createWDBillRecord(RemitOrder $remitOrder)
    {
        $bill_info = "商户提现结算成功，提现订单号[%s]，提现金额[%s]，手续费[%s]，实际扣款[%s]。提款银行信息(提款银行：[%s]，提现卡号：[%s]，提款人姓名：[%s]，支行名称：[%s])";
        $data = [
            'bill_id' => SystemNumber::getBillRecordNumber(BillTypeMap::WITHDRAW),
            'bill_uid' => $remitOrder->uid,
            'bill_type' => 1,
            'bill_channel' => 2,
            'channel_id' => $remitOrder->plat_no,
            'bill_amt' => $remitOrder->ac_money,
            'cur_available' => $remitOrder->platuser->assets->available,
            'bill_info' => sprintf($bill_info,
                $remitOrder->plat_no, $remitOrder->money, $remitOrder->fee,
                $remitOrder->ac_money, BankMap::getNameFromMap($remitOrder->bk_category), $remitOrder->bk_account,
                $remitOrder->bk_username, $remitOrder->bk_branch),
            'created_at' => $remitOrder->updated_at
        ];
        if ($this->create($data)) {
            return true;
        }
        return false;
    }

    public function createTPBillRecord(RemitOrder $remitOrder)
    {
        $bill_info = "商户代付批付号[%s]下的子单号[%s]结算成功，代付金额[%s]，手续费[%s]，实际扣款[%s]。提款银行信息(提款银行：[%s]，提现卡号：[%s]，提款人姓名：[%s]，支行名称：[%s])";
        $data = [
            'bill_id' => SystemNumber::getBillRecordNumber(BillTypeMap::TOPAY),
            'bill_uid' => $remitOrder->uid,
            'bill_type' => 1,
            'bill_channel' => 3,
            'channel_id' => $remitOrder->plat_no,
            'bill_amt' => $remitOrder->ac_money,
            'cur_available' => $remitOrder->platuser->assets->available,
            'bill_info' => sprintf($bill_info,
                $remitOrder->batch_no, $remitOrder->plat_no, $remitOrder->money, $remitOrder->fee,
                $remitOrder->ac_money, BankMap::getNameFromMap($remitOrder->bk_category), $remitOrder->bk_account,
                $remitOrder->bk_username, $remitOrder->bk_branch),
            'created_at' => $remitOrder->updated_at
        ];
        if ($this->create($data)) {
            return true;
        }
        return false;
    }
}